package github.fafaovo.network.Packet;

import github.fafaovo.common.config.exampleconfig;
import github.fafaovo.common.mycapability.ISpeedUpCapability;
import github.fafaovo.core.registry.CommonSetupEventHandler;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.network.PacketBuffer;
import net.minecraft.potion.Effect;
import net.minecraft.potion.EffectInstance;
import net.minecraft.potion.EffectType;
import net.minecraft.potion.Effects;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.fml.network.NetworkEvent;

import java.util.List;
import java.util.Objects;
import java.util.function.Supplier;

public class PowerClinetForServer {


    public float power;

    public PowerClinetForServer() {
    }

    public PowerClinetForServer(float power) {
        this.power = power;
    }

    public float getPower() {
        return power;
    }

    //通过这两个函数序列化和反序列化
    public static void encode(PowerClinetForServer p, PacketBuffer buffer)
    {
        buffer.writeFloat(p.power);
    }

    public static PowerClinetForServer decode(PacketBuffer buffer)
    {
        return new PowerClinetForServer(buffer.readFloat());
    }
    //服务器处理逻辑
    public static void handlePacket(PowerClinetForServer p, Supplier<NetworkEvent.Context> context$)
    {
        NetworkEvent.Context context = context$.get();
        context.enqueueWork(()->{
            ServerPlayerEntity player = context.getSender();
            if(player != null)
            {
                LazyOptional<ISpeedUpCapability> capability = player.getCapability(CommonSetupEventHandler.SPEED_UP_CAPABILITY);
                capability.ifPresent((c)->{
                    float power1 = p.getPower();
                    c.setLevel(power1);
                    pts(player,power1);
                    //example.LOGGER.info(c.getLevel());
                });
            }
        });
        context.setPacketHandled(true);
    }

//    static List<Integer> integers_true = exampleconfig.buff_size_true.get();
//    static List<Integer> integers_false = exampleconfig.buff_size_false.get();

    static boolean aBoolean = exampleconfig.example_boolean.get();
    static List<Integer> integers = aBoolean ? exampleconfig.buff_size_true.get():exampleconfig.buff_size_false.get();

    static int buff_value = exampleconfig.buff_value.get();
    static void pts(ServerPlayerEntity player, float power)
    {
        int result_a = power == 0 ? 1 : (int) power;
        if(result_a >= buff_value)
        {

            //先求满足部分的
            int r = (int) Math.ceil((double)result_a / (integers.size() * buff_value)); //满足条件次数N
            int remain = result_a - ((r - 1) * buff_value * integers.size());
            int beover = remain / buff_value;

            //System.out.println(r - 1 + " " + remain + " " + beover);
            if(r - 1 > 0 )
            {
                for (Integer integer : integers) {
                    //System.out.println(integer + " " + (r - 2));
                    player.addPotionEffect(
                            new EffectInstance(Objects.requireNonNull(Effect.get(integer)),
                                    20*20, Math.min(r - 2, 3)));
                }
            }
            for (int i = 0 ; i < beover ; i++)
            {
                //System.out.println(integers.get(i) + " " + (r - 1));
                player.addPotionEffect(
                        new EffectInstance(Objects.requireNonNull(Effect.get(integers.get(i))),
                                20*20,Math.min(r - 1, 3)));
            }
        }

    }



}
